/*
 * LoginInterceptor.java
 * Copyright (c) 2014, 2019, Kiri. 
 * All rights reserved.
 */
package com.onesalt.web.api.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.onesalt.common.ExceptionConstants;
import com.onesalt.utils.EncryptUtil;

/**
 * @author kiri
 * @date 2015年4月12日
 * @version 1.0
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

	private static final Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String token = request.getParameter("token");
		long userId = EncryptUtil.getUserId(token);
		if (userId == 0) {
			throw ExceptionConstants.NEED_RELOGIN_EXCEPTION;
		}
		logger.debug("userId = " + userId);
		return super.preHandle(request, response, handler);
	}
}
